package com.xiao.bargain.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiao.bargain.entity.BarginEntity;
import com.xiao.bargain.service.BarginService;
import com.xiao.group.common.utils.PageUtils;
import com.xiao.group.common.utils.R;



/**
 * 砍价表
 *
 * @author 韩国涛
 * @email 2845009006@gmail.com
 * @date 2021-09-21 20:37:53
 */
@RestController
@RequestMapping("bargain/bargin")
public class BarginController {
    @Autowired
    private BarginService barginService;

    @RequestMapping("/tolists")
    public R tolists(){
        return R.ok().put("data",barginService.list(new QueryWrapper<BarginEntity>().orderByAsc("huodong_status")));
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = barginService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{barginId}")
    public R info(@PathVariable("barginId") Integer barginId){
		BarginEntity bargin = barginService.getById(barginId);

        return R.ok().put("bargin", bargin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BarginEntity bargin){
		barginService.tosave(bargin);
        tocron();
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BarginEntity bargin){
		barginService.updateById(bargin);
        tocron();
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] barginIds){
		barginService.removeByIds(Arrays.asList(barginIds));

        return R.ok();
    }
    @Scheduled(cron = "0/50 * * * * ?")
    public void  tocron(){
        List<BarginEntity> list = barginService.list();
        for (BarginEntity barginEntity : list) {
            Date date = new Date();
            if(date.getTime() > barginEntity.getStartTime().getTime() &&date.getTime()<barginEntity.getJendTime().getTime()){
                BarginEntity barginEntity1 = barginService.getById(barginEntity.getBarginId());
                barginEntity1.setHuodongStatus("0");
                barginService.updateById(barginEntity);
            }
            if(date.getTime() > barginEntity.getJendTime().getTime()){
                BarginEntity barginEntity1 = barginService.getById(barginEntity.getBarginId());
                barginEntity1.setHuodongStatus("1");
                barginService.updateById(barginEntity1);
            }
            if(date.getTime() < barginEntity.getStartTime().getTime() ){
                BarginEntity barginEntity1 = barginService.getById(barginEntity.getBarginId());
                barginEntity1.setHuodongStatus("2");
                barginService.updateById(barginEntity1);
            }
        }
    }
}
